#include<stdio.h>
#include<dirent.h>
#include<ctype.h>
#include<string.h>

int areDigits(const char *str);
void print_proc(const char *path);

int main() {
	
	print_proc("/proc");
	return 0;
}

void print_proc(const char *path) {
	char buf[128];
	char proc_name[64];

	int length = strlen(path);

	strcpy(buf, path);
	buf[++length] = '/';

	DIR *dir = opendir(path);
	struct dirent *dep;
	while ((dep = readdir(dir)) != NULL) {
		if (areDigits(dep->d_name)) {					
			strcpy(buf + length, dep->d_name);
			strcat(buf, "/comm");
 			FILE *file = fopen(buf, "r");
			if (file != NULL) {
				size_t read_size = fread(proc_name, 1, 64, file);
				proc_name[read_size] = '\0';
				printf("pid = %s, name = %s", dep->d_name, proc_name);
				fclose(file);
			}
			
			
		}
	}
	closedir(dir);

}

int areDigits(const char *str) {
	for (int i = 0; str[i]; i++) {
		if (!isdigit(str[i])) {
			return 0;
		}
	}
	return 1;
}
